import sys

try:
    t = int(input())
    if t < 1 or t > 10:
        sys.exit(0)
    for w in range(t):
        n = int(input())
        aij = []
        for y in range(n):
            aij += [int(item) for item in input().split()]

        q = int(input())
        for z in range(q):
            x = int(input())
            if x in aij:
                ids = [i for i, x1 in enumerate(aij) if x1 == x]
                if len(ids) > 1:
                    cl = [int(c / n) + 1 for c in ids]
                    rl = [int(c % n) + 1 for c in ids]
                    m_ids = [i for i, x2 in enumerate(cl) if x2 == min(cl)]
                    if len(m_ids) > 1:
                        hc = cl[m_ids[len(m_ids) - 1]]
                        hl = rl[m_ids[len(m_ids) - 1]]
                        print("%d %d" % (hc, hl))
                    else:
                        print("%d %d" % (int(min(ids) / n) + 1, int(min(ids) % n) + 1))
                else:
                    h = ids[0]
                    print("%d %d" % (int(h / n) + 1, int(h % n) + 1))

            else:
                print("-1")

except ValueError:
    pass
except EOFError:
    pass
except MemoryError:
    pass